package cn.alm.allinoneboot.service.impl;

import cn.alm.allinoneboot.common.CommonResult;
import cn.alm.allinoneboot.dto.CommonDto;
import cn.alm.allinoneboot.entity.User;
import cn.alm.allinoneboot.mapper.UserMapper;
import cn.alm.allinoneboot.service.UserService;
import cn.alm.allinoneboot.utils.JedisPoolUtil;
import cn.alm.allinoneboot.utils.JwtUtil;
import cn.alm.allinoneboot.utils.Md5Util;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;

    @Override
    public CommonResult info(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        User user = userMapper.selectOne(wrapper);
        CommonResult cr = new CommonResult();
        if (user == null) {
            cr.error();
        } else {
            cr.ok();
            String roleList = user.getRoleList();
            String[] roleArr = null;
            if (roleList != null) {
                roleArr = roleList.split(",");
            }
            user.setRoles(roleArr);
            cr.setData(user);
        }
        return cr;
    }

    @Override
    public CommonResult login(CommonDto dto) throws Exception {
        String username = dto.getUsername();
        String password = dto.getPassword();
        String encryptedPwd = Md5Util.encrypt(password);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        User user = userMapper.selectOne(wrapper);
        CommonResult cr = new CommonResult();
        if (user == null) {
            cr.error();
            cr.setMessage("用户名或密码错误");
            return cr;
        }

        if (!user.getPassword().equals(encryptedPwd)) {
            cr.error();
            cr.setMessage("用户名或密码错误");
            return cr;
        }

        Jedis jedis = JedisPoolUtil.getJedisPoolInstance().getResource();

        String token = jedis.get(user.getUsername());

        if (token == null) {
            token = JwtUtil.createToken(user);
            jedis.set(user.getUsername(), token);
            jedis.expire(user.getUsername(), 3600);
        }
        JedisPoolUtil.release(jedis);

        cr.ok();
        HashMap<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("username", username);
        cr.setData(map);
        return cr;
    }

    @Override
    public CommonResult createUser(CommonDto dto) throws Exception {
        String username = dto.getUsername();
        String password = dto.getPassword();
        String encrypt = Md5Util.encrypt(password);
        User user = new User();
        user.setUsername(username);
        user.setPassword(encrypt);
        user.setId(UUID.randomUUID().toString());
        int insert = userMapper.insert(user);
        CommonResult cr = new CommonResult();
        if (insert == 0) {
            cr.error();
        }
        cr.ok();
        return cr;
    }

    @Override
    public CommonResult logout(CommonDto dto) {
        String username = dto.getUsername();
        Jedis jedis = JedisPoolUtil.getJedisPoolInstance().getResource();
        jedis.del(username);
        CommonResult cr = new CommonResult();
        cr.ok();
        return cr;
    }

}
